package cn.pconline.autoclub.service.bbssync;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.mq.AbstractMessageHandler;
import cn.pconline.autoclub.service.ClubService;
import cn.pconline.autoclub.service.MemberService;
import cn.pconline.autoclub.service.UserService;

import com.alibaba.fastjson.JSONObject;

public abstract class BbsSyncPetrolService extends AbstractMessageHandler {
    @Autowired
    protected ClubService clubService;
    
    @Autowired
    protected MemberService memberService;
    
    @Autowired
    protected UserService userService;
    
    /**
     * 给会员加减油值
     * @param typeId 论坛操作类型
     * @return 油值
     */
    protected abstract int addPetroltoMember(ClubMember clubMember, JSONObject object);
    
    /**
     * 给车友会加减油值
     * @param typeId 论坛操作类型
     * @return 油值
     */
    protected abstract int addPetroltoClub(Club club, JSONObject object);
    
    protected abstract long getUserId(JSONObject object);
    
    protected abstract void process(JSONObject object, ClubMember clubMember);
    
    @Override
    public void handleJsonMessage(JSONObject object) {
        if(object == null) {
            log.warn("the message is empty");
            return;
        }
        //log.info("obj: {}", this);
        if(object.getIntValue("version") < 1) {
            log.info("this version[{}] message that is not need process", object.getIntValue("version"));
            return;
        }
        long forumId = object.getLongValue("forumId");
        if(forumId > 0 && isSkipForum(forumId)) {
            log.info("not need sync forum[{}]", object.getLong("forumId"));
            return;
        }
        //获取用户ID
        long userId = getUserId(object);
        ClubMember clubMember = null;
        if(userId > 0) {
            clubMember = memberService.findClubMemberByUserId(userId);
            log.info("get clubMember by userId[{}]: {}",userId, clubMember);
        } else {
            log.warn("not found userId from msg.");
        }
        //消息预处理
        //preHandle(object);
        //如果没有加入车友会的用户就不存在加油值了
        if(clubMember != null) {
            Club club = clubService.getClub(clubMember.getClubId());
            log.info("get club[{}]: {}", clubMember.getClubId(), club);
            //审核通过的车友会才加油值，待审、锁、封的车友会不加油
            if(club != null && club.getStatus() == Club.STATUS_PASS) {
                //先加车友会再加会员
                //给车友会加减油值
                addPetroltoClub(club, object);
                
                //给会员加减油值
                addPetroltoMember(clubMember, object);
            }
        } else {
            log.info("no init club member or no found club member: {}", userId);
        }
        log.info("start processing message");
        //处理帖子
        process(object, clubMember);
    }
    
    /*
     * 过滤屏蔽版块
     * http://bbs.pcauto.com.cn/forum-15253.html
     * http://bbs.pcauto.com.cn/forum-15252.html
     * http://bbs.pcauto.com.cn/forum-21245.html
     * http://bbs.pcauto.com.cn/forum-19017.html
     * http://bbs.pcauto.com.cn/forum-19375.html
     */
    private boolean isSkipForum(long forumId) {
        long[] filteForums = {15253, 15252, 21245, 19017, 19375};
        if(ArrayUtils.contains(filteForums, forumId)) {
            return true;
        }
        return false;
    }
}
